import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import 'commont/diaButton.dart';
import 'commont/input.dart';
import 'password_change.dart';

enum PasswordInputStyle { setNew, enterOld, setting }

class PasswordInput extends BaseDialog {
  ///保存字符串的key
  String title;
  PasswordInputStyle style;
  String? prefs_key;
  String? password;
  void Function(String)? onOK;

  PasswordInput({super.key, required this.title, required this.style, this.prefs_key, this.password, this.onOK});

  @override
  BaseDialogState createState() => _PasswordInputState();
}

class _PasswordInputState extends BaseDialogState<PasswordInput> {
  final _textControlller = TextEditingController();
  @override
  void initState() {
    super.initState();

    _textControlller.text = '';
    _textControlller.addListener(() {
      String text = _textControlller.text;
    });
  }

  ///按钮响应方法
  void function(String text) async {
    FocusScope.of(context).unfocus();
    switch (text) {
      case 'OK':
        if (_textControlller.text == '' && widget.style != PasswordInputStyle.setting) {
          EasyLoading.showInfo('The password can`t be null');
          return;
        } else if (_textControlller.text != widget.password && widget.style == PasswordInputStyle.enterOld) {
          EasyLoading.showInfo('The password is incorrect');
          return;
        }
        if (widget.style != PasswordInputStyle.setting) {
          saveString(widget.prefs_key ?? '', _textControlller.text);
        }

        Navigator.pop(context);
        if (widget.onOK != null) {
          widget.onOK!(_textControlller.text);
        }
      case 'Cancel':
        // Navigator.pop(context);
        // ChangeNameView changename = ChangeNameView();
        // changename.show(context);
        Navigator.pop(context);
      case 'Change your password':
        Navigator.of(context).pop((reuslt) {});
        PasswordChange password = PasswordChange(oldPassword: widget.password ?? '');
        password.show(context);
        password.onChange = (old, New, again) {
          if (widget.style != PasswordInputStyle.setting) {
            saveString(widget.prefs_key ?? '', New);
          }
          if (widget.onOK != null) {
            widget.onOK!(New);
          }
        };
      default:
    }
  }

  @override
  Widget buildContent(BuildContext context) {
    return LayoutBuilder(builder: (BuildContext context, BoxConstraints constrains) {
      return Container(
        height: 110,
        child: Stack(
          children: [
            ///标题
            Positioned(
                left: 10,
                top: 10,
                height: 40,
                child: Text(
                  widget.title,
                  textAlign: TextAlign.center,
                  style: const TextStyle(fontSize: 14, color: Colors.black),
                )),

            Positioned(
              left: 10,
              right: 10,
              top: 40,
              height: 30,
              child: InputView(controller: _textControlller),
            ),

            Positioned(
                right: 0, top: 70, height: 40, width: 60, child: Diabutton(title: 'OK', onClick: () => function('OK'))),

            Positioned(
                right: 60,
                top: 70,
                height: 40,
                width: 70,
                child: Diabutton(title: 'Cancel', onClick: () => function('Cancel'))),

            Positioned(
                left: 0,
                top: 70,
                height: 40,
                width: widget.style == PasswordInputStyle.enterOld ? 150 : 0,
                child: Diabutton(
                    title: widget.style == PasswordInputStyle.enterOld ? 'Change your password' : '',
                    fontSize: 12,
                    onClick: () => function('Change your password'))),
          ],
        ),
      );
    });
  }
}

// 加载存储的字符串
Future<String> loadString(String key) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  final text = prefs.getString(key) ?? '';
  return text;
}

// 存储字符串到本地
Future<void> saveString(String key, String value) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  await prefs.setString(key, value);
}
